package run.halo.app.controller.admin.api;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import run.halo.app.dto.ResultDTO;
import run.halo.app.dto.request.ToDoReqDTO;
import run.halo.app.enums.ToDoStatusEnum;
import run.halo.app.enums.ToDoTypeEnum;
import run.halo.app.model.entity.TodoList;
import run.halo.app.service.ThemeService;
import run.halo.app.service.TodoListService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("api/admin/todolist")
@Slf4j
public class ToDoListController {

    @Autowired
    ThemeService themeService;

    @Resource(name = "todoListService")
    TodoListService todoListService;

    @ResponseBody
    @PostMapping("/todo")
    public ResultDTO createToDo(@RequestBody ToDoReqDTO toDoReqDTO) {
        String content = toDoReqDTO.getDesc();
        TodoList todoList = new TodoList();
        todoList.setTodoDes(content);
        todoList.setTodoState(0);
        todoList.setTodoType(0);
        TodoList todoList1 = todoListService.saveToDo(todoList);
        if (todoList1 != null) {
            return ResultDTO.success("新增成功", todoList1);
        }
        return ResultDTO.error("新增失败");
    }

    @ResponseBody
    @DeleteMapping("/todo")
    public ResultDTO delToDo(@RequestBody ToDoReqDTO toDoReqDTO) {
        Integer id = toDoReqDTO.getId();
        try{
            todoListService.deleteById(id);
        } catch (Exception e) {
            log.error("删除todo失败");
            return ResultDTO.error("删除失败");
        }
        return ResultDTO.success("删除成功");
    }

    @ResponseBody
    @PatchMapping("/todo")
    public ResultDTO updateToDoState(@RequestBody ToDoReqDTO toDoReqDTO) {

        Integer state = toDoReqDTO.getTodoState();
        Integer id = toDoReqDTO.getId();
        int row = todoListService.updateStatus(id, state);
        if (row == 0) {
            log.error("更新todo状态失败");
            return ResultDTO.error("更新todo状态失败");
        }
        return ResultDTO.success("更新todo状态成功");
    }
}
